package com.windupurnomo;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.windupurnomo.data.ActivityManagerData;
import com.windupurnomo.entity.ListTask;
import com.windupurnomo.entity.ShareInfo;
import com.windupurnomo.entity.TaskDay;
import com.windupurnomo.util.Function;
import com.windupurnomo.util.MyQuery;
import com.windupurnomo.util.Util;

public class ViewDay extends Activity{
	private ShareInfo shareInfo;
	private TextView numDay;
	private ListTask lt = new ListTask();
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.day);
        shareInfo = (ShareInfo) getIntent().getSerializableExtra("shareInfo");
        numDay = (TextView)findViewById(R.id.day);
        String nDay = null;
        switch(shareInfo.getIndexListDay()){
        	case 0: nDay = "First"; break;
        	case 1: nDay = "Second"; break;
        	case 2: nDay = "Third"; break;
        	case 3: nDay = "Forth"; break;
        	case 4: nDay = "Fifth"; break;
        	case 5: nDay = "Sixth"; break;
        	case 6: nDay = "Seventh"; break;
        }
        numDay.setText(nDay);
        int nd = shareInfo.getListDay().get(shareInfo.getIndexListDay());
        Log.i("acmWindu", "nd: "+nd+"\nPlayerStatus: "+shareInfo.getPlayerStatus());
        
        if(shareInfo.getPlayerStatus()==Util.Player.NEW_PLAYER){
        	lt.setTaskDays(getTasks(nd));
        	shareInfo.setTIME(Util.Day.START_DAY);
        }
        else lt = shareInfo.getListTask();
        
		shareInfo.setListTask(lt);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.view_day_menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem){
		switch(menuItem.getItemId()){
			case R.id.menuTask:{
				Intent i = new Intent(this, Tasks.class);
				i.putExtra("shareInfo", shareInfo);
				startActivity(i);
				break;
			}
			case R.id.menuBonus: Toast.makeText(this, "Menu", Toast.LENGTH_LONG).show(); break;
			case R.id.menuWeather: {
				Intent i = new Intent(this, Weather.class);
				i.putExtra("shareInfo", shareInfo);
				startActivity(i);
				break;
			}
			case R.id.menuPlayer: {
				Intent i = new Intent(this, SelectedPlayer.class);
				i.putExtra("shareInfo", shareInfo);
				startActivity(i);
				break;
			}
			case R.id.menuHome:{
				ActivityManagerData acm = new ActivityManagerData(this);
		        SQLiteDatabase db = acm.getWritableDatabase();
				MyQuery.PlayerDAO.updatePlayer(shareInfo.getPlayer(), db);
				Intent i = new Intent(this, Home.class);
				startActivity(i);
				break;
			}
		}
		return true;
	}
	
	private List<TaskDay> getTasks(int day){
		ActivityManagerData acm = new ActivityManagerData(this);
        SQLiteDatabase db = acm.getWritableDatabase();
        List<TaskDay> ltx = MyQuery.TasksDayDAO.getTasks(day, db);
        db.close();
		return Function.randomTaskDay(ltx);
	}
}
